# -*- coding:utf-8 -*-
"""
@Author : 审心
@Time : 2023/4/3 9:49
鼠标相关封装工具
"""
import math
import time
import random
from typing import Tuple

import pyautogui
from typing import Tuple


def scope_random_tap(coordinate: Tuple[int, int], size: Tuple[float, float], delay: int = 0,
                     reduce_scope: Tuple[int, int] = (1, 1)):
    """
    范围内随机点击
    :param coordinate:范围左上角坐标 x,y
    :param size: 矩形大小 0 : 宽 1:高
    :param delay: 点击后延迟
    :param reduce_scope: 缩小随机范围 (x,y)
    :return:
    """
    tap(x=random.uniform(coordinate[0] + reduce_scope[0], coordinate[0] + size[0] - reduce_scope[0]),
        y=random.uniform(coordinate[1] + reduce_scope[1], coordinate[1] + size[1] - reduce_scope[1]), delay=delay)


def tap(x, y, duration: float = 0.15, delay: int = 0):
    """
    左键点击
    :param x: x坐标
    :param y: y坐标
    :param duration: 模拟拖动鼠标时的每步延迟
    :param delay: 点击后延迟
    :return:
    """
    pyautogui.click(x, y, duration=duration)
    time.sleep(delay)


def move(x: int, y: int, duration: float = 0.2):
    """
    将鼠标移动到指定位置
    :param x: x坐标
    :param y: y坐标
    :param duration: 每部间隔
    :return:
    """
    pyautogui.moveTo(x, y, duration=duration)


def drag(start: Tuple[int, int], end: Tuple[int, int], duration: float = 1, delay: float = 0):
    """
    鼠标滑动
    :param start: 启示位置坐标
    :param end: 结束位置坐标
    :param duration: 滑动间隔
    :param delay: 结束延迟
    :return:
    """
    move(start[0], start[1])
    pyautogui.mouseDown()
    move(end[0], end[1], duration=duration)
    pyautogui.mouseUp()
    time.sleep(delay)


if __name__ == '__main__':
    drag((839, 349), (1091, 350))
